package com.panshi.controller;

import com.panshi.entity.AppUserT;
import com.panshi.result.R;
import com.panshi.service.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserClientService userClientService;

    @GetMapping("/login")
    public String login(){
        return "login/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public R login(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        Object kaptcha = session.getAttribute("kaptcha");   //获取保存的session验证码
        String rcode = request.getParameter("rcode");       //获取前台拿到的图形验证码
        R rt = new R();
        if(!rcode.equalsIgnoreCase(kaptcha+"")){
            rt.setCode(301);
            rt.setMessage("请输入正确图形验证码");
            return rt;
        }
        String type = request.getParameter("type"); //是否是验证码登录
        String code = request.getParameter("code"); //验证码

        if(type.equals(1)){
            if(code.isEmpty()){
                rt.setCode(301);
                rt.setMessage("请输入正确验证码");
                return rt;
            }
        }

        String user_name = request.getParameter("user_name");
        String user_password = request.getParameter("user_password");
        //封装成实体
        AppUserT appUserT = new AppUserT().setUserStatus(type).setUserEmail(user_name).setUserPassword(user_password).setUserSex(code);
        System.out.println("用户登录-------------------》》");
        System.out.println(appUserT);

        R r = userClientService.login(appUserT);
        if(r.getCode()==301){
            return r;
        }
        System.out.println(r);
        int userid = (int)r.getData();
        AppUserT userInfo = userClientService.byid(userid);
        System.out.println(userInfo);
        if(r.getCode()==200){
            System.out.println("登录成功   保存用户数据到cookie");
            Cookie cookie = new Cookie("userBank",user_name);
            Cookie cookie1 = new Cookie("userPassword",userInfo.getUserPassword());
            Cookie cookie2 = new Cookie("userType",type);
            Cookie cookieid = new Cookie("userId", userInfo.getUserId()+"");
            cookie.setMaxAge(60*60*24);
            cookie1.setMaxAge(60*60*24);
            cookie2.setMaxAge(60*60*24);
            cookieid.setMaxAge(60*60*24);
            response.addCookie(cookie);
            response.addCookie(cookie1);
            response.addCookie(cookie2);
            response.addCookie(cookieid);
        }

        return r;
    }


    @GetMapping("/login_register")
    public String loginPhone(){
        return "login/login_register";
    }

    @GetMapping("/login_pass")
    public String loginPass(){
        return "login/login_pass";
    }

}
